// component/send_star/send_star.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showNotice: false,
    opacity: 1,
    currentinfo: {},
  },

  /**
   * 组件生命周期函数，在组件实例进入页面节点树时执行
   */
  attached: function () {
    this.getDollRecords();
  },

  /**
   * 组件生命周期函数，在组件实例被从页面节点树移除时执行
   */
  detached: function () {
    clearInterval(this.interval);
    clearTimeout(this.reloadTimeout);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //获取首页的抓取记录
    getDollRecords: function () {
      app.Ajax({
        url: '/api/index.php?app=home&act=doll_records',
        success: (res) => {
          if (res) {
            this.dollList = res;
            this.dollIndex = 0;
            //循环显示中奖用户
            this.controlOpacity();
            if (this.interval) {
              clearInterval(this.interval);
            }
            this.interval = setInterval(this.controlOpacity.bind(this), 14000)
            //该轮用户显示完后再次查询中奖用户列表
            this.reloadTimeout = setTimeout(() => {
              this.getDollRecords();
            }, 14000 * this.dollList.length)
          }
        },
      })
    },
    controlOpacity: function () {
      if (this.dollIndex > this.dollList.length - 1) {
        clearInterval(this.interval);
        return;
      }
      var currentinfo = this.dollList[this.dollIndex];
      this.dollIndex++;
      this.setData({
        currentinfo: currentinfo,
        showNotice: true,
      })

      setTimeout(() => {
        this.setData({
          opacity: 0,
        })
      }, 2000);
      setTimeout(() => {
        this.setData({
          showNotice: false,
          opacity: 1,
        })
      }, 4000);
    },
  },
})
